<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">

            <el-form-item label="水机名称" prop="search">
                <el-input
                    v-model="queryParams.search"
                    placeholder="请输入水机名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                >新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table  v-loading="loading"  :data="currentPageData" border fit highlight-current-row>
            <el-table-column type="selection" width="55" bo align="center" label-width="100px" />
            <el-table-column label="产品编号" align="center" prop="MID"  />
            <el-table-column label="主板名称" align="center" prop="EquipmentName"  />
            <el-table-column label="主板型号" align="center" prop="ModelName"  />

            <el-table-column label="在线状态" align="center"  >
                <template slot-scope="{row}">
                    <el-tag  type="danger">{{ handleOnline(row) }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  >
                <template slot-scope="{row}">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(row)">编辑名称</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            layout="prev, pager, next,jumper, ->, total"
            :total="Lists.length"
            @current-change="handleCurrentChange"
            :current-page="this.queryParams.page"
            :page-size="this.queryParams.limit"
            style="text-align: center">
        </el-pagination>
        <!-- 绑定设备 对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px"  >
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="主板编号" prop="mid">
                    <el-input v-model="form.mid" placeholder="请输入主板编号"/>
                </el-form-item>
                <el-form-item label="水机名称" prop="title">
                    <el-input v-model="form.title" placeholder="请输入水机名称"/>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>


import { getAllCom} from "@/api/grzq/community.js";
import {getApparatusList,getApparatusJundaAllList,updateApparatus}from "@/api/grzq/apparatus.js";

import {removeFrom} from "@/utils";
import {getApparatusJdList,createApparatusJd,updateApparatusJd,getOnlineStatusM} from "@/api/grzq/apparatus_junda";


export default {
    name: "Apparatus",
    dicts:['sys_normal_disable'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 表格数据
            Lists: [],
            total: 0,
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                page: 1,
                limit: 10,
            },
            form:{},
            rules:{
                mid: [
                    { required: true, message: '主板编号不能为空', trigger: 'blur' }
                ],
                title: [
                    { required: true, message: '主板名称不能为空', trigger: 'blur' }
                ],
            },

        };
    },
    computed:{
        //当前页数据
        currentPageData() {
            const start = (this.queryParams.page - 1) * this.queryParams.limit;
            const end = this.queryParams.page * this.queryParams.limit;
            return this.Lists.slice(start, end);
        },
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询部门列表 */
        async getList() {
            let that = this;
            that.loading = true;
            const {data, status, msg} = await getApparatusJdList(this.queryParams);
            that.Lists = data;
            that.loading = false;
            that.total = data.total;
        },
        handleCurrentChange(currentPage) {
            this.currentPage = currentPage; //每次点击分页按钮，当前页发生变化
        },
        async handleOnline(row){
            // const {data} = await getOnlineStatusM({mid:row.MID})
            // let name = '';
            // if (data.isOnline){
            //     name = '在线'
            // }else {
            //     name = '离线'
            // }
            console.log("data",name)
            row.online ='状态'
        },


        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset(){
            this.form = {
                mid: 0,
                title: ''
            };
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd(row) {
            this.reset();
            this.open = true;
            this.title = "添加数据";
        },
        /** 新增按钮操作 */
        handleUpdate(row) {
            this.reset();
            this.open = true;
            this.form.mid = row.MID;
            this.form.title = row.EquipmentName;
        },

        submitForm: function() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.mid != undefined) {
                        updateApparatusJd(removeFrom(this.form)).then(response => {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        })
                    } else {
                        createApparatusJd(removeFrom(this.form)).then(response => {
                            this.msgSuccess('新增成功')
                            this.goBack()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            this.confirm('是否确认删除名称为"' + row.title + '"的数据项？').then(function() {
                return delMaster({read_id:row.read_id});
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(() => {});
        }
    }
};
</script>
